using System;

namespace BubbleSortDemo
{
    /// <summary>
    /// 学生类
    /// </summary>
    public class Student
    {
        // 私有字段
        private string _studentId;
        private string _name;
        private int _age;
        private double _score;

        // 公共属性
        /// <summary>
        /// 学号
        /// </summary>
        public string StudentId
        {
            get => _studentId;
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    throw new ArgumentException("学号不能为空");
                _studentId = value;
            }
        }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get => _name;
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    throw new ArgumentException("姓名不能为空");
                _name = value;
            }
        }

        /// <summary>
        /// 年龄
        /// </summary>
        public int Age
        {
            get => _age;
            set
            {
                if (value < 0 || value > 150)
                    throw new ArgumentOutOfRangeException(nameof(value), "年龄必须在0-150之间");
                _age = value;
            }
        }

        /// <summary>
        /// 成绩
        /// </summary>
        public double Score
        {
            get => _score;
            set
            {
                if (value < 0 || value > 100)
                    throw new ArgumentOutOfRangeException(nameof(value), "成绩必须在0-100之间");
                _score = value;
            }
        }

        // 构造函数
        /// <summary>
        /// 无参构造函数
        /// </summary>
        public Student()
        {
            StudentId = "000000";
            Name = "未知";
            Age = 0;
            Score = 0;
        }

        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="studentId">学号</param>
        /// <param name="name">姓名</param>
        /// <param name="age">年龄</param>
        /// <param name="score">成绩</param>
        public Student(string studentId, string name, int age, double score)
        {
            StudentId = studentId;
            Name = name;
            Age = age;
            Score = score;
        }

        // 方法
        /// <summary>
        /// 显示学生信息
        /// </summary>
        public void ShowInfo()
        {
            Console.WriteLine($"学号: {StudentId}");
            Console.WriteLine($"姓名: {Name}");
            Console.WriteLine($"年龄: {Age}");
            Console.WriteLine($"成绩: {Score}");
            Console.WriteLine($"等级: {GetGrade()}");
            Console.WriteLine();
        }

        /// <summary>
        /// 根据成绩获取等级
        /// </summary>
        /// <returns>成绩等级</returns>
        public string GetGrade()
        {
            if (Score >= 90) return "优秀";
            if (Score >= 80) return "良好";
            if (Score >= 70) return "中等";
            if (Score >= 60) return "及格";
            return "不及格";
        }

        /// <summary>
        /// 更新成绩
        /// </summary>
        /// <param name="newScore">新成绩</param>
        public void UpdateScore(double newScore)
        {
            Score = newScore;
            Console.WriteLine($"{Name}的成绩已更新为: {Score}");
        }

        /// <summary>
        /// 重写ToString方法
        /// </summary>
        /// <returns>学生信息字符串</returns>
        public override string ToString()
        {
            return $"Student[学号: {StudentId}, 姓名: {Name}, 年龄: {Age}, 成绩: {Score}, 等级: {GetGrade()}]";
        }

        /// <summary>
        /// 比较两个学生的成绩
        /// </summary>
        /// <param name="other">另一个学生对象</param>
        /// <returns>比较结果</returns>
        public int CompareScore(Student other)
        {
            if (other == null)
                throw new ArgumentNullException(nameof(other));
            
            return Score.CompareTo(other.Score);
        }
    }
}
